﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;

using SimpleText.Framework;

public partial class LanguageVersioningPage : SimpleTextPage
{
	protected Language EditLanguage = null;

  protected void Page_Load(object sender, EventArgs e)
  {
		InitPage();

		if (!IsPostBack)
		{
			SaveLanguageBtn.Attributes.Add("onclick", "if ( ! validateLanguageForm()) { return false;	}");
			EditBtn.Attributes.Add("onclick", "if ( ! validateLanguageForm()) { return false;	}");
		}

		if (CurrentModel == null)
		{
			Response.Redirect("projects.aspx?msg=noprojectselected");
		}
		if (Request.QueryString["mode"] == "delete")
		{
			DeleteLanguage(Request.Form["language"]);
		}
		if (Request.QueryString["mode"] == "setmaster")
		{
			SetMasterLanguage(Request.Form["language"]);
		}
		
		if (Request.QueryString["mode"] == "edit")
		{
			foreach (Language lang in CurrentModel.Languages)
			{
				if (lang.ShortName == Request.QueryString["language"])
				{
					EditLanguage = lang;
					break;
				}
			}
			if (!IsPostBack)
			{
				AddHeadlinePlh.Visible = false;
				EditHeadlinePlh.Visible = true;
				CreateBtnPlh.Visible = false;
				EditBtnPlh.Visible = true;
				EditNameLit.Text = EditLanguage.ShortName;
				ShortNameTxt.Value = EditLanguage.ShortName;
				NameTxt.Value = EditLanguage.Name;
				CultureSel.Value = EditLanguage.CultureName;
			
			}
		}

		//languages
		LanguageRpt.DataSource = CurrentModel.Languages;
		LanguageRpt.DataBind();

	}

	protected void DeleteLanguage(string shortName)
	{
		foreach (Language lang in CurrentModel.Languages)
		{
			if (lang.ShortName == shortName)
			{
				CurrentModel.Languages.Remove(lang);
				break;
			}
		}

		foreach (TextDefinition definition in CurrentModel.TextDefinitions.Values)
		{
			definition.Texts.Remove(shortName);
		}

		SaveModel(CurrentModel, CurrentModel.LoadedFromXmlFilePath);

		Response.Redirect("languages.aspx?msg=deletesuccess");
	}
	protected void SetMasterLanguage(string shortName)
	{
		CurrentModel.MasterLanguageShortName = shortName;
		SaveModel(CurrentModel, CurrentModel.LoadedFromXmlFilePath);
		Response.Redirect("languages.aspx?msg=masterset");
	}
	protected void SaveLanguageBtnClicked(object sender, EventArgs a)
	{
		bool isEdit = (EditLanguage != null);

		Language language = null;
		string originalShortName = null;
		string newShortName = ShortNameTxt.Value;
		if (isEdit)
		{
			language = EditLanguage;
			originalShortName = EditLanguage.ShortName;
		}
		else
		{
			language = new Language();
		}
		language.CultureName = CultureSel.Value;
		language.Name = NameTxt.Value;
		language.ShortName = newShortName;

		if (isEdit)
		{
			if (newShortName != originalShortName)
			{
				//if this is the current master language, update the name reference
				if (CurrentModel.MasterLanguageShortName == originalShortName)
				{
					CurrentModel.MasterLanguageShortName = newShortName;
				}
				//update all texts that uses this short name (short name is the primary key used by the texts)

				foreach (string definitionKey in CurrentModel.TextDefinitions.Keys)
				{
					if (CurrentModel.TextDefinitions[definitionKey].Texts.ContainsKey(originalShortName))
					{
						CurrentModel.TextDefinitions[definitionKey].Texts[originalShortName].LanguageShortName = newShortName;
					}
				}

				//reload the model (otherwise the dictionary will still se the old name as keys
				LoadModel(CurrentModel.LoadedFromXmlFilePath);
			}
		}
		else
		{
			CurrentModel.Languages.Add(language);
		}

		SaveModel(CurrentModel, CurrentModel.LoadedFromXmlFilePath);
		if (isEdit)
		{
			Response.Redirect("languages.aspx?msg=languageedited");
		}
		else
		{
			Response.Redirect("languages.aspx?msg=languageadded");
		}
	}

	/*
	http://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo%28v=vs.71%29.aspx
	af	Afrikaans
	af-ZA	Afrikaans - South Africa
	sq	Albanian
	sq-AL	Albanian - Albania
	ar	Arabic
	ar-DZ	Arabic - Algeria
	ar-BH	Arabic - Bahrain
	ar-EG	Arabic - Egypt
	ar-IQ	Arabic - Iraq
	ar-JO	Arabic - Jordan
	ar-KW	Arabic - Kuwait
	ar-LB	Arabic - Lebanon
	ar-LY	Arabic - Libya
	ar-MA	Arabic - Morocco
	ar-OM	Arabic - Oman
	ar-QA	Arabic - Qatar
	ar-SA	Arabic - Saudi Arabia
	ar-SY	Arabic - Syria
	ar-TN	Arabic - Tunisia
	ar-AE	Arabic - United Arab Emirates
	ar-YE	Arabic - Yemen
	hy	Armenian
	hy-AM	Armenian - Armenia
	az	Azeri
	az-AZ-Cyrl	Azeri (Cyrillic) - Azerbaijan
	az-AZ-Latn	Azeri (Latin) - Azerbaijan
	eu	Basque
	eu-ES	Basque - Basque
	be	Belarusian
	be-BY	Belarusian - Belarus
	bg	Bulgarian
	bg-BG	Bulgarian - Bulgaria
	ca	Catalan
	ca-ES	Catalan - Catalan
	zh-HK	Chinese - Hong Kong SAR
	zh-MO	Chinese - Macau SAR
	zh-CN	Chinese - China
	zh-CHS	Chinese (Simplified)
	zh-SG	Chinese - Singapore
	zh-TW	Chinese - Taiwan
	zh-CHT	Chinese (Traditional)
	hr	Croatian
	hr-HR	Croatian - Croatia
	cs	Czech
	cs-CZ	Czech - Czech Republic
	da	Danish
	da-DK	Danish - Denmark
	div	Dhivehi
	div-MV	Dhivehi - Maldives
	nl	Dutch
	nl-BE	Dutch - Belgium
	nl-NL	Dutch - The Netherlands
	en	English
	en-AU	English - Australia
	en-BZ	English - Belize
	en-CA	English - Canada
	en-CB	English - Caribbean
	en-IE	English - Ireland
	en-JM	English - Jamaica
	en-NZ	English - New Zealand
	en-PH	English - Philippines
	en-ZA	English - South Africa
	en-TT	English - Trinidad and Tobago
	en-GB	English - United Kingdom
	en-US	English - United States
	en-ZW	English - Zimbabwe
	et	Estonian
	et-EE	Estonian - Estonia
	fo	Faroese
	fo-FO	Faroese - Faroe Islands
	fa	Farsi
	fa-IR	Farsi - Iran
	fi	Finnish
	fi-FI	Finnish - Finland
	fr	French
	fr-BE	French - Belgium
	fr-CA	French - Canada
	fr-FR	French - France
	fr-LU	French - Luxembourg
	fr-MC	French - Monaco
	fr-CH	French - Switzerland
	gl	Galician
	gl-ES	Galician - Galician
	ka	Georgian
	ka-GE	Georgian - Georgia
	de	German
	de-AT	German - Austria
	de-DE	German - Germany
	de-LI	German - Liechtenstein
	de-LU	German - Luxembourg
	de-CH	German - Switzerland
	el	Greek
	el-GR	Greek - Greece
	gu	Gujarati
	gu-IN	Gujarati - India
	he	Hebrew
	he-IL	Hebrew - Israel
	hi	Hindi
	hi-IN	Hindi - India
	hu	Hungarian
	hu-HU	Hungarian - Hungary
	is	Icelandic
	is-IS	Icelandic - Iceland
	id	Indonesian
	id-ID	Indonesian - Indonesia
	it	Italian
	it-IT	Italian - Italy
	it-CH	Italian - Switzerland
	ja	Japanese
	ja-JP	Japanese - Japan
	kn	Kannada
	kn-IN	Kannada - India
	kk	Kazakh
	kk-KZ	Kazakh - Kazakhstan
	kok	Konkani
	kok-IN	Konkani - India
	ko	Korean
	ko-KR	Korean - Korea
	ky	Kyrgyz
	ky-KZ	Kyrgyz - Kazakhstan
	lv	Latvian
	lv-LV	Latvian - Latvia
	lt	Lithuanian
	lt-LT	Lithuanian - Lithuania
	mk	Macedonian
	mk-MK	Macedonian - FYROM
	ms	Malay
	ms-BN	Malay - Brunei
	ms-MY	Malay - Malaysia
	mr	Marathi
	mr-IN	Marathi - India
	mn	Mongolian
	mn-MN	Mongolian - Mongolia
	no	Norwegian
	nb-NO	Norwegian (Bokml) - Norway
	nn-NO	Norwegian (Nynorsk) - Norway
	pl	Polish
	pl-PL	Polish - Poland
	pt	Portuguese
	pt-BR	Portuguese - Brazil
	pt-PT	Portuguese - Portugal
	pa	Punjabi
	pa-IN	Punjabi - India
	ro	Romanian
	ro-RO	Romanian - Romania
	ru	Russian
	ru-RU	Russian - Russia
	sa	Sanskrit
	sa-IN	Sanskrit - India
	sr-SP-Cyrl	Serbian (Cyrillic) - Serbia
	sr-SP-Latn	Serbian (Latin) - Serbia
	sk	Slovak
	sk-SK	Slovak - Slovakia
	sl	Slovenian
	sl-SI	Slovenian - Slovenia
	es	Spanish
	es-AR	Spanish - Argentina
	es-BO	Spanish - Bolivia
	es-CL	Spanish - Chile
	es-CO	Spanish - Colombia
	es-CR	Spanish - Costa Rica
	es-DO	Spanish - Dominican Republic
	es-EC	Spanish - Ecuador
	es-SV	Spanish - El Salvador
	es-GT	Spanish - Guatemala
	es-HN	Spanish - Honduras
	es-MX	Spanish - Mexico
	es-NI	Spanish - Nicaragua
	es-PA	Spanish - Panama
	es-PY	Spanish - Paraguay
	es-PE	Spanish - Peru
	es-PR	Spanish - Puerto Rico
	es-ES	Spanish - Spain
	es-UY	Spanish - Uruguay
	es-VE	Spanish - Venezuela
	sw	Swahili
	sw-KE	Swahili - Kenya
	sv	Swedish
	sv-FI	Swedish - Finland
	sv-SE	Swedish - Sweden
	syr	Syriac
	syr-SY	Syriac - Syria
	ta	Tamil
	ta-IN	Tamil - India
	tt	Tatar
	tt-RU	Tatar - Russia
	te	Telugu
	te-IN	Telugu - India
	th	Thai
	th-TH	Thai - Thailand
	tr	Turkish
	tr-TR	Turkish - Turkey
	uk	Ukrainian
	uk-UA	Ukrainian - Ukraine
	ur	Urdu
	ur-PK	Urdu - Pakistan
	uz	Uzbek
	uz-UZ-Cyrl	Uzbek (Cyrillic) - Uzbekistan
	uz-UZ-Latn	Uzbek (Latin) - Uzbekistan
	vi	Vietnamese
	vi-VN	Vietnamese - Vietnam 
	*/
}
